/* eslint-disable @angular-eslint/component-class-suffix */
import { HttpClient } from '@angular/common/http';
import { Component,OnInit,Inject } from '@angular/core';
import { MatDialog,MatDialogRef,MAT_DIALOG_DATA } from '@angular/material/dialog';
import {FormGroup, FormControl} from '@angular/forms';
import * as moment from 'moment';

// import { DeviceTagDialog } from './../../device/device.component';

@Component({
    selector: 'app-device-detail',
    templateUrl: './device-detail.component.html',
    styleUrls: ['./device-detail.component.scss']
})
export class DeviceDetailComponent implements OnInit {


    constructor(private dialog: MatDialog,
        public dialogRef: MatDialogRef<DeviceDetailComponent>,
        private http: HttpClient,
        @Inject(MAT_DIALOG_DATA) public data: any) {
            console.log(data);
            this.deviceId=data.deviceId;
        this.loadData();
    }

    detail: any={};
    measureList: any[];
    deviceId: string;

    loadData() {
            this.http.get(`/deviceApi/device/mgt/getDeviceInfo/${this.deviceId}?tenantId=1369559970221985794`).subscribe((res: any)=>{
                if(res.code===200){
                    this.detail=res.data;
                    this.http.post(`/deviceApi/device/mgt/measureDefinitionListPage?tenantId=1369559970221985794`,
                    {
                        keyword:'',
                        current:1,
                        size:1000,
                        deviceId:this.deviceId,
                        dataQueryFlag:true
                    }).subscribe((res: any)=>{
                        if(res.code===200){
                            this.measureList = res.data.list;
                        }
                    });

                }
              });


    }

    getDesc(measure){
        let str='--';
        if(measure.metricValue){
            str=measure.metricValue;
        }
        let definition= measure.dataDefinitions.find(dd=>{dd.value===measure.metricValue;});
        if(definition){
            str= `(${definition.description})`;
        }
        return str;
    }

    onMeasure(measure){
        console.log(measure);
        let dialogRef = this.dialog.open(DialogMeasureList, {
            position: { top: '60px' },
            data: {
                measure,
                device:this.detail
            }
        });

        dialogRef.afterClosed().subscribe(result => {

        });
    }

    getTooltip(m){
        let str='合理范围：';
        return str + (m.min && m.max)?'合理范围：'+m.min+'～'+m.max:'~';
    }

    ngOnInit(): void {
        console.log('init');
    }

    onNoClick(){
        this.dialogRef.close();
    }

}

const today = new Date();
const month = today.getMonth();
const year = today.getFullYear();

@Component({
    selector: 'dialog-measure-list',
    templateUrl: './measure-list.dialog.html',
    styleUrls: ['./device-detail.component.scss']
})
export class DialogMeasureList {
    constructor(public dialogRef: MatDialogRef<DialogMeasureList>,
        private http: HttpClient,
        @Inject(MAT_DIALOG_DATA) public data: any) {
            console.log(data);
            this.getData();
    }


    displayedColumns: string[] = ['time', 'value'];
    dataSource: any[]=[];

    campaignOne = new FormGroup({
        start: new FormControl(moment().subtract(2,'days').format('YYYY-MM-DD 00:00:00') ),
        end: new FormControl(moment().format('YYYY-MM-DD 23:59:59')),
      });



    getData(){
        let startTime='';
        let endTime='';
        if(this.campaignOne.value && this.campaignOne.value.start && this.campaignOne.value.end){
            startTime=moment(this.campaignOne.value.start).format('YYYY-MM-DD 00:00:00');
            endTime=moment(this.campaignOne.value.end).format('YYYY-MM-DD 23:59:59');
        }
        let param={
            startTime:startTime,
            endTime:endTime,
            deviceId:this.data.measure.deviceId,
            measureCode:this.data.measure.code,
            entityTypeCode:this.data.device.entityTypeCode,
            source:this.data.device.source || 'custom',
            queryFlag:2
        };
        this.http.post('/deviceApi/device/report/getHistoryMeasureData?tenantId=1369559970221985794',param).subscribe((res: any)=>{
            if(res.code===200){
                this.dataSource=res.data;
            }

        });
    }

    onNoClick(): void {
        this.dialogRef.close();
    }

}
